<template>
  <div class="login-container">
    <el-card class="login-card">
      <div class="header">
        <h2>医院管理系统</h2>
        <p>请登录您的账号</p>
      </div>
      
      <el-form :model="loginForm" :rules="rules" ref="loginFormRef">
        <el-form-item prop="role">
          <el-select v-model="loginForm.role" placeholder="请选择用户类型" class="form-input">
            <el-option label="管理员" value="admin"></el-option>
            <el-option label="医生" value="doctor"></el-option>
            <el-option label="患者" value="patient"></el-option>
          </el-select>
        </el-form-item>
        
        <el-form-item prop="id">
          <el-input v-model="loginForm.id" placeholder="请输入账号" class="form-input"></el-input>
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" class="form-input"></el-input>
        </el-form-item>
        
        <div class="form-actions">
          <el-button type="primary" :loading="loading" @click="handleLogin" class="login-btn">登录</el-button>
        </div>
        
        <div class="form-links">
          <router-link to="/register">注册账号</router-link>
          <router-link to="/forgot-password">忘记密码</router-link>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useAuthStore } from '../../stores/auth'
import { ElMessage } from 'element-plus'

const authStore = useAuthStore()
const loading = ref(false)
const loginFormRef = ref(null)

const loginForm = reactive({
  role: 'patient',
  id: '',
  password: ''
})

const rules = {
  role: [{ required: true, message: '请选择用户类型', trigger: 'change' }],
  id: [{ required: true, message: '请输入账号', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  try {
    await loginFormRef.value.validate()
    loading.value = true
    
    await authStore.login(loginForm)
    
    if (authStore.error) {
      ElMessage.error(authStore.error)
    }
  } catch (validationError) {
    console.error('Validation error:', validationError)
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
}

.login-card {
  width: 400px;
  padding: 20px;
}

.header {
  text-align: center;
  margin-bottom: 20px;
}

.header h2 {
  font-size: 24px;
  color: #303133;
  margin-bottom: 8px;
}

.header p {
  color: #909399;
  font-size: 14px;
}

.form-input {
  width: 100%;
}

.form-actions {
  margin-top: 24px;
}

.login-btn {
  width: 100%;
}

.form-links {
  margin-top: 16px;
  display: flex;
  justify-content: space-between;
  font-size: 14px;
}

.form-links a {
  color: #409eff;
  text-decoration: none;
}
</style> 